package com.hcinfos.gsoo.ui;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

import com.easemob.chatuidemo.db.UserDao;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.User;
import com.hcinfos.gsoo.util.DialogUtil;
import com.hcinfos.gsoo.util.SharePreferenceUtil;
import com.hcinfos.gsoo.util.UtilHttp;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyDetailActivity extends Activity {

	private ImageView back_iv;
	private TextView gg_tv;
	private TextView nick_tv;
	private TextView credit_tv;
	private TextView email_tv;
	private TextView tel_tv;
	private TextView address_tv;
	private User user;

	private ObjectMapper mapper;
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_detail);
		mapper = new ObjectMapper();
		back_iv = (ImageView) findViewById(R.id.back_iv);
		dialog = DialogUtil.createProgressDialog(this, "提示", "用户信息获取中...");

		back_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});

		SharePreferenceUtil util = new SharePreferenceUtil(this, "setting");
		user = (User) util.loadObject("user");
		String ggNum = getIntent().getStringExtra("ggNumber");
		gg_tv = (TextView) findViewById(R.id.gg_tv);
		nick_tv = (TextView) findViewById(R.id.nick_tv);
		credit_tv = (TextView) findViewById(R.id.credit_tv);
		email_tv = (TextView) findViewById(R.id.email_tv);
		tel_tv = (TextView) findViewById(R.id.tel_tv);
		address_tv = (TextView) findViewById(R.id.address_tv);

		if (ggNum != null) {
			dialog.show();
			obtainUser(ggNum);
		} else {
			if (user != null) {
				updateView();
			}
		}

	}

	public final int OBTAIN_SUCCESS = 0x2210;
	public final int OBTAIN_FAIL = OBTAIN_SUCCESS + 1;

	private void obtainUser(String ggNum) {

		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		params.addBodyParameter("beanProxy.ggnumber", ggNum);

		http.send(HttpRequest.HttpMethod.POST, Urls.call_findLikeUser, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						handler.sendEmptyMessage(OBTAIN_FAIL);

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {

						String result = arg0.result;
						Log.d("wxf", "result=" + result);
						try {

							user = mapper.readValue(result,
									new TypeReference<User>() {
									});
							handler.sendEmptyMessage(OBTAIN_SUCCESS);

						} catch (Exception e) {
							Log.e("wxf", e.toString());
							handler.sendEmptyMessage(OBTAIN_FAIL);

						}

					}

				});
	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			dialog.dismiss();
			switch (msg.what) {
			case OBTAIN_SUCCESS:
				updateView();
				break;
			case OBTAIN_FAIL:
				Toast.makeText(MyDetailActivity.this, "获取失败!", 500).show();
				finish();
				break;
			}

		};
	};

	private void updateView() {

		UserDao dao = new UserDao(this);

		String gg = user.getGgnumber();
		if (gg == null)
			gg = "暂无";
		gg_tv.setText("即搜号：" + gg);
		String nick = user.getUsername();
		if (nick == null)
			nick = "暂无";
		if (user.getTaName() != null) {
			nick = user.getTaName();
		}
		if (nick != null) {
			dao.updateUser(user.getGgnumber(), nick);
		}
		nick_tv.setText("昵  称：" + nick);

		String email = user.getEmail();
		if (email == null) {
			email = "暂无";
		}
		email_tv.setText("邮  箱：" + email);

		Integer god = user.getGold();
		if (god == null) {
			god = 0;
		}
		credit_tv.setText("积  分：" + god + "");

		String tel = user.getTaTelphone();
		if (tel == null) {
			tel = "暂无";
		}
		tel_tv.setText("电  话：" + tel);
		String address = user.getSendAddr1();
		if (address == null) {
			address = "暂无";
		}
		address_tv.setText("地  址：" + address);
	}
}
